package com.heima.article.service.impl;

import com.heima.article.interceptor.AppUserIdHolder;
import com.heima.article.service.ArticleBehaviorService;
import com.heima.common.constants.BehaviorConstants;
import com.heima.common.redis.CacheService;
import com.heima.model.article.dtos.ArticleinfoDto;
import com.heima.model.article.dtos.CollectionBehaviorDto;
import com.heima.model.article.vos.ArticleinfoVo;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Set;

@Service
public class ArticleBehaviorServiceImpl implements ArticleBehaviorService {
    @Autowired
    private CacheService cacheService;

    /**
     * 收藏或取消收藏
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult collectionBehavior(CollectionBehaviorDto dto) {
        //校验参数
        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //组合key
        String key = BehaviorConstants.BEHAVIOR_COLLECTION + dto.getEntryId();
        //判断类型
        if (dto.getOperation() == BehaviorConstants.BEHAVIOR_USER_COLLECTION) {
            //收藏,存入文章id和用户id
            cacheService.sAdd(key, AppUserIdHolder.get());

        } else if (dto.getOperation() == BehaviorConstants.BEHAVIOR_USER_NOTCOLLECTION) {
            //取消收藏
            cacheService.sRemove(key, AppUserIdHolder.get());
        }

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 行为回显
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult articleBehaviorService(ArticleinfoDto dto) {
        //校验参数
        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //创建返回类型
        ArticleinfoVo articleinfoVo = new ArticleinfoVo();
        //文章id
        Long articleId = dto.getArticleId();
        //获取自媒体用户id
        Integer authorId = dto.getAuthorId();
        //获取用户
        String userId = AppUserIdHolder.get();
        //拼接关注key
        String followKey = BehaviorConstants.BEHAVIOR_FOLLOW + authorId;
        Set<String> followKeys = cacheService.setMembers(followKey);
        for (String string : followKeys) {
            if(!string.equals(userId)){
                articleinfoVo.setIsfollow(false);
            }
            articleinfoVo.setIsfollow(true);
        }
        //拼接喜欢key
        String likeItKey = BehaviorConstants.BEHAVIOR_LIKE_IT + articleId;
        Set<String> likeItKeys = cacheService.setMembers(likeItKey);
        for (String string : likeItKeys) {
            if(!string.equals(userId)){
                articleinfoVo.setIslike(false);
            }
            articleinfoVo.setIslike(true);
        }
        //拼接不喜欢key
        String disLikeKey = BehaviorConstants.BEHAVIOR_DISLIKE + articleId;
        Set<String> disLikeKeys = cacheService.setMembers(disLikeKey);
        for (String string : disLikeKeys) {
            if(!string.equals(userId)){
                articleinfoVo.setIsunlike(false);
            }
            articleinfoVo.setIsunlike(true);
        }
        //拼接收藏key
        String collection = BehaviorConstants.BEHAVIOR_COLLECTION + articleId;
        Set<String> collections = cacheService.setMembers(collection);
        for (String string : collections) {
            if(!string.equals(userId)){
                articleinfoVo.setIscollection(false);
            }
            articleinfoVo.setIscollection(true);
        }

        return ResponseResult.okResult(articleinfoVo);
    }
}
